import {http, HttpResp} from "./global.ts";
import {MessageType} from "./common.ts";

export interface ConversationMenuPayload {
    conversationId: string;
    conversationType: string;
    title: string;
}

export interface ConversationContent {
    type: MessageType;
    content: string;
}

export interface ConversationPayload {
    conversationId: string;
    conversationType: string;
    title: string;
    datasource: string;
    tableName: string;
    messages: ConversationContent[];
}


export function createConversation(conversationType: string) {
    return http.get<ConversationMenuPayload, HttpResp<ConversationMenuPayload>>(
        "/conversation/create",
        {
            params: {conversationType: conversationType},
        },
    );
}


export function editConversation(conversationId: string, title: string) {
    return http.put<unknown, HttpResp>(
        "/conversation/edit",
        {
            conversationId: conversationId,
            title: title,
        },
        {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
            },
        },
    );
}


export function listConversation() {
    return http.get<
        ConversationMenuPayload[],
        HttpResp<ConversationMenuPayload[]>
    >("/conversation/list");
}


export function getConversation(conversationId: string) {
    return http.get<ConversationPayload, HttpResp<ConversationPayload>>(
        "/conversation/get",
        {
            params: {conversationId: conversationId},
        },
    );
}


export function deleteConversation(conversationId: string) {
    return http.delete<unknown, HttpResp>(
        "/conversation/del", {
            params: {conversationId: conversationId},
        });
}

export function train(values  ) {
    const formData = new FormData();
    formData.append('conversationId', values.conversationId);
    formData.append('trainType', values.trainType);
    formData.append('question', values.question);
    formData.append('content', values.content);
    formData.append('datasource', values.datasource);
    formData.append('tableName', values.tableName);
    return http.post<string, HttpResp<string>>('/chat/train', formData, {
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    });
}











